/*
Brick And Blast, a crossplatform breakout game.
 Copyright (C) 2008-2010 Julien CLEMENT (andematunum@gmail.com)
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "Brick.h"

/**
* file   Brick.cpp
* date   Sam 9 jan 2010 10:58:25 CET
* brief  TODO
*/

#include "Level.h"
#include "Settings.h"

Brick::
Brick(BrickType type, BrickType next_type, SDL_Point& position)
  : GameObject(GAME_OBJECT_BRICK,GAME_OBJECT_RECTANGLE,position,
                 Level::get_brick_texture(type)),
      type_(type), next_type_(next_type), size_init_(size_.r) {
   set_position(position);
}

void
Brick::
handle_collision (GameObject* obj, const ColliderData& data) {
 if ((type_ <= BRICK_PURPLE) && (type_ > BRICK_BLUE)) {
    type_ = static_cast<BrickType>((int)type_ - 0x1);
 }
 else if (type_ == BRICK_BLUE) {
  // Check the next_type field
  if (next_type_ == BRICK_NONE) {
    if ((double)random() / (double)RAND_MAX < Settings::get_chance_brick_nrj()) {
      type_ = BRICK_NRJ;
      next_type_ = BRICK_NONE;
      set_texture(Level::get_brick_texture(type_));
    }
    else {
      type_ = BRICK_NONE;
    }
  }
  else {
    type_ = next_type_;
    next_type_ = BRICK_NONE;
  }
 }
 else if (type_ == BRICK_NRJ) {
  cout << "NRJ collected !" << endl;
  type_ = BRICK_NONE;
 }
 else {
  cout << "Not yet handled !" << endl;
 }

 if (type_ == BRICK_NONE) {
  set_active(false);
 }
 else {
  set_texture(Level::get_brick_texture(type_));
 }
}

